#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

//仅仅知识了解一下,序列化和反序列化的过程

typedef struct request
{
    int left;
    int right;
    char op;
}request_t;

void serialization()
{
    request req = {10, 20, '+'};
    //是一个万金油式的对象,可以承载很多的对象
    Json::Value root;

    //Json是一种kv式的序列化方案
    root["data_left"] = req.left;
    root["data_right"] = req.right;
    root["operator"] = req.op;

    //两种不同风格的序列化
    Json::FastWriter fwriter;
    Json::StyledWriter swriter;
    std::string json_string_f = fwriter.write(root);
    std::string json_string_s = swriter.write(root);

    std::cout << json_string_f << std::endl;
    std::cout << json_string_s << std::endl;
}

void deserialization()
{
    //这里的R"(......)",表示里面的字符串是原始字符串字面量,这就意味着字符串中的特殊字符(比如双引号和反斜杠)不需要转义
    std::string json_string = R"({"data_left" : 10, "data_right" : 20, "operator" : 42})";

    Json::Reader reader;
    Json::Value root;

    reader.parse(json_string, root);

    request_t req;
    req.left = root["data_left"].asInt();
    req.right = root["data_right"].asInt();
    req.op = (char)root["operator"].asInt();

    std::cout << req.left << " " << req.op << " " << req.right << std::endl;
}

int main()
{
    //序列化的过程
    //serialization();
    
    //反序列化的过程
    deserialization();
}
